package com.lookme.lmtool.model;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.Serializable;

/**
 * 字符特征
 * @author blake
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@XStreamAlias("charAspect")
public class CharAspect implements Serializable {

    /**
     * 字符内容
     */
    @XStreamAsAttribute
    private String key;
    /**
     * 字符的特征码
     */
    @XStreamAsAttribute
    private String value;
    /**
     * 字符的宽度
     */
    @XStreamAsAttribute
    private int width;
    /**
     * 字符的高度
     */
    @XStreamAsAttribute
    private int height;

    /**
     * 字符特征图像
     */
    private transient BufferedImage image;

    public BufferedImage getImage() {
        if(image==null){
            BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
            for(int y=0;y<height;y++){
            for(int x=0;x<width;x++){
                    if(value.charAt(y*width+x)=='1'){
                        bufferedImage.setRGB(x,y, Color.black.getRGB());
                    }else{
                        bufferedImage.setRGB(x,y, Color.white.getRGB());
                    }
                }
            }
            image=bufferedImage;
        }
        return image;
    }
}
